/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conexion;

import Modelo.Datos;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author dayana
 */
public class datos_DAO {

    public static List<Datos> consultarDatos(String fecha) {
        ConexionBD conexion = new ConexionBD();
        Statement s = null;
        ResultSet rs = null;

        String query = "SELECT * FROM datos WHERE fecha='" + fecha + "';";

        System.out.println(query);

        try {
            s = conexion.session.createStatement();
        } catch (SQLException se) {
            System.out.println("Error Consultando Datos! " + se.getMessage());
            return null;
        }

        try {
            rs = s.executeQuery(query);

            List<Datos> datos = new ArrayList();
            try {
                int i = 0;
                while (rs.next()) {
                    Datos dato = new Datos(rs.getInt(1), "" + rs.getDate(2), "" + rs.getTime(3), rs.getDouble(4), rs.getDouble(5), rs.getDouble(6), rs.getDouble(7), rs.getDouble(8));
                    datos.add(dato);

                }
            } catch (Exception e) {
                System.out.println("Excepcion: " + e);
            }

            return datos;
        } catch (SQLException se) {
            se.printStackTrace();
            System.out.println("Error Consultando Datos2! " + se.getMessage());
            return null;
        } finally {
            if (conexion != null) {
                conexion.cerrarConexion();
            }
        }
    }

    public static String AlmacenarDatos(Datos fila) {
        ConexionBD conexion = new ConexionBD();
        Statement s = null;
        String Resp = "fallo";
        DateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");

        try {
            s = conexion.session.createStatement();
        } catch (SQLException se) {
            System.out.println("Error Estableciendo Conexion para Insertar un Dato! " + se.getMessage());
            return Resp;
        }

        System.out.println("Fecha: " + fila.getFecha() + " "
                + "hora: " + fila.getHora() + " "
                + "presion: " + fila.getPresionAtm() + " "
                + "temp: " + fila.getTemperatura() + " "
                + "viento: " + fila.getViento() + " "
                + "humedad: " + fila.getHumedad() + " "
                + "precipit: " + fila.getPrecipitacion());
        try {

            String query = "INSERT INTO datos "
                    + "(fecha, hora, presion_atmosferica, temperatura,"
                    + " viento, humedad, precipitacion) "
                    + "VALUES ("
                    + "'" + fila.getFecha() + "',"
                    + "'" + fila.getHora() + "',"
                    + fila.getPresionAtm() + ","
                    + fila.getTemperatura() + ","
                    + fila.getViento() + ","
                    + fila.getHumedad() + ","
                    + fila.getPrecipitacion() + ");";

            //      System.out.println(query);


            s.executeUpdate(query);
            Resp = "ok";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error al insertar dato " + e.getMessage());
        } finally {
            if (conexion != null) {
                conexion.cerrarConexion();
            }
            return Resp;
        }
    }

    public static boolean Autenticar(String user, String pass) {
        boolean valido = false;
        ConexionBD conexion = new ConexionBD();
        Statement s = null;
        String query = "";
        ResultSet RS;

        try {
            s = conexion.session.createStatement();
        } catch (SQLException se) {
            System.out.println("Error Estableciendo Conexion para Insertar un Dato! " + se.getMessage());
            return false;
        }

        try {
            query = "SELECT * FROM usuarios WHERE usuario='" + user
                    + "' AND pass= '"+ pass + "';";

            System.out.println(query);

            RS = s.executeQuery(query);
            System.out.println("Paso del RS");
            while (RS.next()) {
                valido = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error al consultar user " + e.getMessage());
        } finally {
            if (conexion != null) {
                conexion.cerrarConexion();
            }
            return valido;
        }
    }
}
